/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.micda.moroccoby.front.form;

import com.micda.moroccoby.common.dao.UtilisateurDAO;
import com.micda.moroccoby.common.entities.Utilisateur;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;

/**
 *
 * @author Administrateur
 */
@ManagedBean(name = "authentificationBean")
@RequestScoped
public class AuthentificationBean {

    /**
     * Creates a new instance of AuthentificationBean
     */
    
    @EJB
    private UtilisateurDAO utilisateurDAO;
    
    private String email;
    private String password;
    
    public AuthentificationBean() {
    }
    
    public void login(){
        Utilisateur u = utilisateurDAO.getUtilisateurByEmail(email);
        if(u != null){
            if(u.getCompte().getPassword().equals(password)){
                HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(true);
                session.setAttribute("UserLogged", u);
            }
        }
    }
    
    public String logout(){
        HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(true);
        session.removeAttribute("UserLogged");
        return "";
    }

    /**
     * @return the email
     */
    public String getEmail() {
        return email;
    }

    /**
     * @param email the email to set
     */
    public void setEmail(String email) {
        this.email = email;
    }

    /**
     * @return the password
     */
    public String getPassword() {
        return password;
    }

    /**
     * @param password the password to set
     */
    public void setPassword(String password) {
        this.password = password;
    }
    
    
    
    
}
